import 'dart:math';
import 'package:flutter/material.dart';

class AnimatedBackground extends StatefulWidget {
  final Widget child;

  const AnimatedBackground({
    Key? key,
    required this.child,
  }) : super(key: key);

  @override
  _AnimatedBackgroundState createState() => _AnimatedBackgroundState();
}

class _AnimatedBackgroundState extends State<AnimatedBackground> with TickerProviderStateMixin {
  late AnimationController _controller;
  late List<HeartParticle> _particles;
  final int _particleCount = 15;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 10),
    )..repeat();

    _particles = List.generate(_particleCount, (_) => HeartParticle());
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final primaryColor = Theme.of(context).primaryColor;

    return AnimatedBuilder(
      animation: _controller,
      builder: (context, _) {
        return Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                Colors.white,
                primaryColor.withOpacity(0.1),
                Colors.white,
              ],
              stops: [
                0.0,
                _controller.value,
                1.0,
              ],
            ),
          ),
          child: Stack(
            children: [
              // 粒子效果
              ...List.generate(_particles.length, (index) {
                final particle = _particles[index];
                final position = particle.getPosition(_controller.value);
                final size = particle.size * (0.5 + (_controller.value * 0.5));

                return Positioned(
                  left: position.dx,
                  top: position.dy,
                  child: Opacity(
                    opacity: 0.3 * particle.opacity * (1 - _controller.value),
                    child: Icon(
                      Icons.favorite,
                      color: primaryColor,
                      size: size,
                    ),
                  ),
                );
              }),

              // 主内容
              widget.child,
            ],
          ),
        );
      },
    );
  }
}

class HeartParticle {
  final double startX;
  final double startY;
  final double endX;
  final double endY;
  final double size;
  final double opacity;

  HeartParticle()
      : startX = Random().nextDouble() * 400,
        startY = Random().nextDouble() * 800,
        endX = Random().nextDouble() * 400,
        endY = Random().nextDouble() * 800,
        size = 10 + Random().nextDouble() * 20,
        opacity = 0.3 + Random().nextDouble() * 0.7;

  Offset getPosition(double progress) {
    return Offset(
      startX + (endX - startX) * progress,
      startY + (endY - startY) * progress,
    );
  }
}

